﻿using AutoMapper;
using MediatR;
using S.Y.Passport.Domain.溯源;
using S.Y.Passport.ErroeCode;
using S.Y.Passport.Infacet;
using S.Y.Passport.Read.API.Applications.Commands;
using System.Net.WebSockets;

namespace S.Y.Passport.Read.API.Applications.CommandHandler
{
    public class GoodsAddCommandHandler : IRequestHandler<GoodsAddCommand, SyResult<object>>
    {
        private readonly IBaseRepo<GoodsInfo> goodsInfoRepo;
        private readonly IMapper mapper;

        public GoodsAddCommandHandler(IBaseRepo<GoodsInfo>goodsInfoRepo,IMapper mapper) 
        {
            this.goodsInfoRepo = goodsInfoRepo;
            this.mapper = mapper;
        }
        public Task<SyResult<object>> Handle(GoodsAddCommand request, CancellationToken cancellationToken)
        {
            SyResult<object> result = new SyResult<object>();

            DateTime date = DateTime.Now;
            string dateString = date.ToString("yyyyMMdd"); // 格式化为年-月-日
            string day = date.ToString("yyyyMMddHHmmss");

            string combinedString = dateString;
            string combinedDay = dateString+ day;

            result.Msg = SyCode.添加成功.ToString();
            result.Code = SyCode.添加成功;

            var entity = mapper.Map<GoodsInfo>(request);

            // 拼接日期和时间
            entity.Manufactory = combinedString;
            entity.Licence = $"SC{combinedDay}";
            entity.CreateName = "张三";
            entity.CreateTime = date;

            goodsInfoRepo.Add(entity);

            result.Data = entity;

            return Task.FromResult(result);
        }
    }
}
